package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.ManageMapper;
import com.cyzy.pojo.AdminUser;
import com.cyzy.pojo.RoleMenu;
import com.cyzy.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ManageServiceImpl extends ServiceImpl<ManageMapper, AdminUser> implements ManageService {
    @Autowired
    private ManageMapper manageMapper;
    @Override
    public AdminUser selectAcc(String username) {
        LambdaQueryWrapper<AdminUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminUser ::getUsername,username);
        return this.getOne(queryWrapper);
    }

    @Override
    public int updateManage(String username, Integer roleId, Integer adminState, Timestamp createTime) {
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", username); // 条件：匹配 username

        AdminUser admin = new AdminUser();
        if (roleId != null) {
            admin.setRoleId(roleId);
        }
        if (adminState != null) {
            admin.setAdminState(adminState);
        }
        if (createTime != null) {
            admin.setCreateTime(createTime);
        }

        return manageMapper.update(admin, updateWrapper);
    }

    @Override
    public int deleteState(String name, String state) {
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", name); // 条件：匹配 name

        AdminUser admin = new AdminUser();
        admin.setAdminState(2); // 状态改为 2

        return manageMapper.update(admin, updateWrapper);
    }

    @Override
    public int deleteStateByIds(List<String> ids, String state) {
        UpdateWrapper<AdminUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("admin_id", ids); // 条件：ID 在传入的列表中

        AdminUser admin = new AdminUser();
        admin.setAdminState(2); // 状态设为 2 表示删除（逻辑删除）

        return manageMapper.update(admin, updateWrapper); // 返回受影响行数
    }
}
